/* Copyright 2013-2015 www.snakerflow.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.parser.logicflow.node;

import org.apache.commons.lang.math.NumberUtils;
import org.snaker.engine.model.NodeModel;
import org.snaker.engine.model.SubProcessModel;
import org.snaker.engine.parser.logicflow.LogicFlowJSONAbstractNodeParser;
import org.snaker.engine.parser.logicflow.model.LogicFlowData;


/**
 * 子流程节点解析类
 *
 * @author yuqs
 * @since 1.0
 */
public class SubProcessParser extends LogicFlowJSONAbstractNodeParser {

    /**
     * 产生SubProcessModel模型对象
     */
    protected NodeModel newModel() {
        return new SubProcessModel();
    }

    @Override
    protected void parseNode(NodeModel model, LogicFlowData.Node node) {
        SubProcessModel subProcessModel = (SubProcessModel) model;
        LogicFlowData.Properties properties = node.getProperties();
        String version = properties.get(ATTR_VERSION);
        int ver = 0;
        if (NumberUtils.isNumber(version)) {
            ver = Integer.parseInt(version);
        }
        //TODO 对子流程还不是很了解后续实践中再进行理解
        // 目前根据描述是对模块化流程步骤，抽取一部分流程作为子流程然后再使用时直接使用子流程节点引用定义的子流程进行执行
        // 目前的LogicFlow设计器给的结构是把分组放在children中暂时还没有想好怎么进行适配
        subProcessModel.setSubProcessIds(node.getChildren());
        subProcessModel.setVersion(ver);
        String form = properties.get(ATTR_FORM);
        subProcessModel.setForm(form);

    }

}
